package cn.web.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

//@WebFilter("/*" )

public class LoginFilter implements Filter{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        String requestURI = request.getRequestURI();
        if(requestURI.contains("/login.jsp")||requestURI.contains("/css/")||requestURI.contains("/js/")||requestURI.contains("/fonts/")||requestURI.contains("/checkCodeServlet")||requestURI.contains("/loginServlet")){
            filterChain.doFilter(servletRequest,servletResponse);
        }else {
            Object user = request.getSession().getAttribute("user");
            if(user!=null){
                filterChain.doFilter(servletRequest,servletResponse);
            }else{
                request.setAttribute("login_respon","你还没有登录呢，请先登陆好吗");
                request.getRequestDispatcher("/login.jsp").forward(request,servletResponse);
            }

        }
        //放行

    }

    @Override
    public void destroy() {

    }
}
